package com.harlan.viewbindingstudy.base.mvp;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * @author Jock.Lin
 * @description 
 * 2016-5-26 下午4:22:43
 */
public  class ActivityUtils {

    private static List<Activity> mActivityList = new ArrayList<Activity>();

    private static final String MY_PKG_NAME = "com.weipai.main";

    public static void add(Activity activity) {

            mActivityList.add(activity);



    }

    public static void remove(Activity activity) {

        mActivityList.remove(activity);
    }
    public static void finishActivity(Activity activity) {
        if(mActivityList.contains(activity)){//改
            mActivityList.remove(activity);
        }

    }
    public static List<Activity> getAtyList(){
    	return mActivityList;
    }
    /**
     * finish所有的存活的Activity
     * 并杀死应用进程
     */
    public static void finishKill() {
        finishAll();
        android.os.Process.killProcess(android.os.Process.myPid());
    }
    /**
     * finish所有的存活的Activity
     */
    public static void finishAll() {
        for (Activity activity : mActivityList) {
            activity.finish();
        }
    }

    /**
     * finish所有的存活的Activity
     */
    public static void finishAllExceptSomeOne(String activityName) {

        Log.e("backinfo","不被结束的activity的名字为-------------》"+activityName);
        for (Activity activity : mActivityList) {
            if(!activityName.contains(activity.getLocalClassName())){
                activity.finish();
            }
        }
    }

    /**
     * 结束activity栈顶 几个
     */
    public static void finishActivityByNumber(int size) {

        int mSize = mActivityList.size();
        for (int i =1;i<size;i++) {

            if(mSize - i>=0){
                Activity activity = mActivityList.get(mSize-i);
                activity.finish();
            }
        }
    }
//    /**
//     * 判断是否有activity栈中是否存在某个activity
//     */
//    public static boolean judgeHaveSomeOne(String activityName) {
//
//        for (Activity activity : mActivityList) {
//            if(activityName.contains(activity.getLocalClassName())){
//               return true;
//            }
//        }
//        return false;
//    }
    /**
     * 判断activity栈中的某个位置的activity是否是想要的activity
     */
    public static boolean judgeHaveSomeOne(String activityName, int position) {

       Activity activity = mActivityList.get(position);

        return activityName.contains(activity.getLocalClassName());

    }



    /**
     * 参数index
     * 获取某个Activtivity
     * 有的话返回 此Activity
     * 没有的话返回 null
     */
    public static String getActivityName(int index){

        if(mActivityList.size()-1>=index){
            return mActivityList.get(index).getLocalClassName();
        }
        return  null;
    }
    /**
     * 获取Activity栈中activity的个数
     */
    public static int getActivitiseSize(){
        return mActivityList.size();
    }
    public static boolean isApprunin(Context context){
    	
        boolean isAppRunning = false;
        int pid = android.os.Process.myPid();
        ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> list = am.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo info : list) {
            if (info.pid == pid) {
                if (info.processName.equals(MY_PKG_NAME)){
                    isAppRunning = true;
                    break;
                }
            }
        }
        return isAppRunning;
    }
}
